package cn.sxt.sys.listener;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.UserService;

/**
 * 工作流的办理人的监听
 * 这里面不能加注解 因这个类的对象是avtiviti使用new去创建的
 * @author lijun
 *
 */
public class LeaveBillTaskListener implements TaskListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateTask delegateTask) {
		//1,得到request
		ServletRequestAttributes requestAttributes=(ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		//2,得到session
		HttpSession session = request.getSession();
		//3,得到当前用户
		User user=(User) session.getAttribute("user");
		//4,得到领导id： mgr
		Integer mgr = user.getMgr();
		//5,根据IOC容器
		WebApplicationContext context = 
				WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
		//6,得到userService， 去查领导信息
		UserService userService = context.getBean(UserService.class);
		//7，查领导信息
		User leaderUser = userService.queryUserById(mgr);
		//8,设置办理人
		delegateTask.setAssignee(leaderUser.getName());
	}
	
}
